package com.lhf.fvsgenerator.util;

/**
 * <p></p>
 *
 * @author lhf
 * @since 2020/10/23 15:09
 */
public class PathUtil {

    private static final String A = "\\\\";
    private static final String B = "/";
    private static final String C = "\\";

    /**
     * 路径后斜杠
     *
     * @param path
     * @return
     */
    public static String deleteSlash(String path) {
        StringBuilder sb = new StringBuilder(path);
        int index = isLastSlash(path);
        if (index != -1) {
            sb.deleteCharAt(index);
        }
        return sb.toString();
    }

    /**
     * 最后一个字符是否为资源分隔符 \\  /
     *
     * @param path
     * @return
     */
    public static int isLastSlash(String path) {


        if (path.lastIndexOf(A) == path.length() - 2) {
            return path.lastIndexOf(A);
        }

        if (path.lastIndexOf(B) == path.length() - 1) {
            return path.lastIndexOf(B);
        }

        if (path.lastIndexOf(C) == path.length() - 1) {
            return path.lastIndexOf(C);
        }


        return -1;
    }

    /**
     * 转包名
     * @param path
     * @return
     */
    public static String toPackage(String path) {
        String slash = deleteSlash(path);
        return slash.replaceAll(A, ".").replaceAll(B, ".").replaceAll(C, ".");
    }
}
